package com.qcby.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.front.model.SgArticle;
import com.qcby.front.model.SgCategory;
import com.qcby.front.dao.SgCategoryMapper;
import com.qcby.front.service.SgArticleService;
import com.qcby.front.service.SgCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.framwork.utils.ResponseResult;
import com.qcby.front.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-04
 */
@Service
public class SgCategoryServiceImpl extends ServiceImpl<SgCategoryMapper, SgCategory> implements SgCategoryService {
    @Autowired
    private SgArticleService sgArticleService;
    @Autowired
    private SgCategoryService sgCategoryService;
    @Autowired
    private SgCategoryMapper sgCategoryMapper;
    @Override
    public ResponseResult getCategoryList() {
        //查询已经发布文章的表
        LambdaQueryWrapper<SgArticle> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(SgArticle::getStatus,"0");
        List<SgArticle> articleList = sgArticleService.list(articleWrapper);
        //获取发布文章的分类id
        List<Long> categoryIds =new ArrayList<>();
        for(SgArticle sgArticle:articleList){
            categoryIds.add(sgArticle.getCategoryId());
        }
        //根据分类id查询这个分类的状态是否正常（0）
        QueryWrapper<SgCategory> sgCategoryQueryWrapper=new QueryWrapper<>();
        for(int i=0;i<categoryIds.size();i++){
            if(sgCategoryService.getById(categoryIds.get(i)).getStatus().equals('0')){
                sgCategoryQueryWrapper.eq("id",categoryIds.get(i));
            }else{
                continue;
            }
        }
        //最后封装返回前端的对象
        List<SgCategory> sgCategories= sgCategoryService.list(sgCategoryQueryWrapper);
        List<CategoryVO>  categoryVOS=new ArrayList<>();
        for(SgCategory sgCategory:sgCategories){
            CategoryVO categoryVO=new CategoryVO();
            categoryVO.setId(sgCategory.getId());
            categoryVO.setName(sgCategory.getName());
            categoryVOS.add(categoryVO);
        }
        return ResponseResult.okResult(categoryVOS);
    }
}
